package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;


@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String passerword = user.getPassword();
        String md5 = DigestUtils.md5DigestAsHex(passerword.getBytes());
        user.setPassword(md5);
        User userDB = userMapper.selectOne(new QueryWrapper<User>(user));
        if(userDB == null){
            return null;
        }else {
            //使用uuid动态生成token,根据当前时间毫秒数+随机数  生成
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            return token;
        }
    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        Page<User> userPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());  //判断是否有值
        userQueryWrapper.like(flag,"username",pageResult.getQuery());
        userPage = userMapper.selectPage(userPage, userQueryWrapper);
        pageResult.setTotal(userPage.getTotal()).setRows(userPage.getRecords());
        return pageResult ;
    }
    //@Override
    //public PageResult getUserList(PageResult pageResult) {
    //    long total = userMapper.selectCount(null);
    //    int size = pageResult.getPageSize();
    //    int start = (pageResult.getPageNum()-1)*size;
    //    List<User> listByPage = userMapper.findListByPage(start,size);
    //    pageResult.setTotal(total).setRows(listByPage);
    //    return pageResult;
    //
    //}

    /**
     *  规则:根据对象中不为null的元素当作set条件
     *      Id当作唯一where条件
     */
    @Override
    @Transactional
    public void updateStatus(User user) {
        userMapper.updateById(user);
    }

    /**
     * 用户入库操作需要手动补齐
     *      1.创建的时间/修改的时间保证一致
     *        status = true
     *      2.密码加密处理
     *        新增和登录密码加密算法必须一致
     * @param user
     */
    @Override
    public void addUser(User user) {
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password).setStatus(true);
        userMapper.insert(user);
    }

    @Override
    @Transactional
    /**
     * @Transactional规则说明:
     *      1.如果发生运行时异常,则实现事务回滚
     *      2.如果发生编译异常(检查异常),默认规则事务不回滚
     *      3.属性说明:
     *          noRollbackFor={}  遇到某种异常不回滚
     *          rollbackFor={}  遇到某周异常回滚
     */
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public User findUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateById(user);
    }


}
